<?php
namespace app\admin\controller;

use think\Db;
use think\Log;
use think\Request;
use think\Validate;

class Parents extends Common
{

    /**
     * 列表
     */
    public function index(){
        if(Request::instance()->isAjax()){
            $input = array_filter(input('','','trim'));
            return \app\common\model\Parents::getList($input);
        }
        return view('',[]);
    }

    /**
     * 删除
     */
    public function delete(){
        try{
            $id = input('id');
            $role = \app\common\model\Parents::where(['id'=>$id])->delete();
            if ($role){
                return ['code'=>'0','msg'=>'删除成功'];
            }else{
                return ['code'=>'1','msg'=>'删除失败'];
            }
        }catch (\Exception $exception) {
            return json(['code' => 1, 'msg' => $exception->getMessage(), 'data' => []]);
        }
    }


    /**
     * 导出
     * 导出excel
     */
    public function export()
    {
        try {
            $data = array_filter(input('','','trim'));
            $data= \app\common\model\Parents::getList($data);
            if ($data['code'] != 0) {
                throw new \think\Exception("查询失败");
            }
            vendor("PHPExcel");
            vendor("PHPExcel.Writer.Excel2007");
            vendor("PHPExcel.IOFactory");

            //列名
            $header = array( '昵称','联系方式', '性别','注册时间');
            $field = ['nickname','mobile','gender_alias','create_time'];

            if (!empty($data["data"])) {
                $name_list =[];
                array_walk($data['data'], function($value, $key) use (&$name_list ,$field){
                    foreach ($field as $val){
                        $name_list [$key][$val] = $value[$val];
                    }
                });
                getExcel("家长表".date('YmdHis'), $header, $name_list);
            } else {
                return '没有相关数据';
            }
        }catch(\Exception $e) {
            Log::notice($e);
            return $e->getMessage();
        }
    }


}
